<?php

namespace App\Support\CustomForm;

use App\Enums\WorkOrder\MissionEnum;
use App\Models\WorkOrder\WorkOrderApplyMainModel;
use App\Models\WorkOrder\WorkOrderMissionModel;
use App\Service\WorkOrder\Reject\WorkOrderRejectService;
use Illuminate\Support\Facades\Cache;

class AttributeWorkOrderSupport extends AttributeSupport
{
    const INPUT_TYPE_TEXT             = "text";
    const INPUT_TYPE_NUMBER           = "number";
    const INPUT_TYPE_MOBILE           = "mobile";
    const INPUT_TYPE_TEXTAREA         = 'textarea';
    const INPUT_TYPE_SELECT_MULTIPLE  = 'select-multiple'; //弹出 - 多选框
    const INPUT_TYPE_SELECT           = "select"; //下拉 - 单选框
    const INPUT_DATE                  = "date";
    const INPUT_DATETIME              = "datetime";
    const INPUT_TYPE_REGIONAL_LINKAGE = "address"; // 地区联动
    const INPUT_TYPE_ALBUM_RADIO      = 'albumRadio'; // 相册单选，默认
    const INPUT_TYPE_ALBUM_CHECKBOX   = 'albumCheckbox'; // 相册多选
    const INPUT_TYPE_CAMERA_RADIO     = 'cameraRadio'; // 相机单选
    const INPUT_TYPE_CAMERA_CHECKBOX  = 'cameraCheckbox'; // 相机多选
    const INPUT_TYPE_VIDEO_RADIO      = 'videoRadio'; // 视频单选
    const INPUT_TYPE_VIDEO_CHECK_BOX  = 'videoCheckbox'; // 视频多选
    const INPUT_TYPE_FILE_CHECK_BOX   = 'fileCheckbox'; // 文件多选
    const INPUT_TYPE_IMG_VIDEO_CHECK_BOX   = 'imgVideo'; // 图片/视频多选

    const REJECT_OPTIONS_IMAGE = [
        '图片不清晰',
        '图片不符合规范',
        '图片和要求内容不一致',
        '非法人名下银行卡',
        '其他',
    ];

    const REJECT_OPTIONS_VIDEO = [
        '视频不清晰',
        '视频不符合规范',
        '视频和要求内容不一致',
        '非法人名下银行卡',
        '其他',
    ];

    const REJECT_OPTIONS_TEXT = [
        '内容描述不清晰',
        '内容不符合规范',
        '内容和要求不一致',
        '其他'
    ];

    public static ?WorkOrderApplyMainModel $applyMainModel = null;

    public static function setApplyMainModel(string $workOrderNo, ?WorkOrderApplyMainModel $applyMainModel = null)
    {
        if (!self::$applyMainModel) {

            if ($applyMainModel) {
                self::$applyMainModel = $applyMainModel;
            }

            if ($workOrderNo) {
                self::$applyMainModel = WorkOrderApplyMainModel::query()->where('workOrderNo', $workOrderNo)->first();
            }
        }
    }

    public static function setSelectOptions(array $enums): array
    {
        $arr = [];
        foreach ($enums as $k => $v) {
            $arr[] = [
                'text'  => $v,
                'value' => $k
            ];
        }

        return $arr;
    }

    //问题类型枚举
    public static function setSelectOptionsToString(array $enums): array
    {
        $arr = [];
        foreach ($enums as $k => $v) {
            $arr[] = [
                'text'  => $v,
                'value' => (string)$k
            ];
        }

        return $arr;
    }

    /**
     * 驳回按钮
     * @return AttributeSupport
     */
    public static function doReject($attributeSupport)
    {
        if (self::$applyMainModel) {
            list($rejectDetails, $dealUids) = Cache::store('array')
                                                   ->remember("doReject:" . self::$applyMainModel->workOrderNo, 60,
                                                       function () {
                                                           $rejectDetails = (new WorkOrderRejectService(self::$applyMainModel->workOrderNo))->detailReject();

                                                           // 当前业务处理人
                                                           $dealUids = WorkOrderMissionModel::query()
                                                                                            ->where('workOrderNo',
                                                                                                self::$applyMainModel->workOrderNo)
                                                                                            ->where('missionStatus',
                                                                                                MissionEnum::MISSION_STATUS_0)
                                                                                            ->pluck('dealUid')
                                                                                            ->toArray();

                                                           return [$rejectDetails, $dealUids];
                                                       });

        } else {
            $rejectDetails = collect();
            $dealUids      = [];
        }

        $field       = $attributeSupport->getInputName();
        $reject      = $rejectDetails->where('field', $field)->first();
        $rejectCount = $rejectDetails->count();
        $userId      = getRmsUserId();

        if ($rejectCount == 0 && in_array($userId, $dealUids)) {
            // 增加驳回按钮
            $attributeSupport->setCustomAttribute(['reject', true]);
        }
        switch ($attributeSupport->getInputType()) {
            case self::INPUT_TYPE_STRING:
            case self::INPUT_TYPE_NUMBER:
            case self::INPUT_TYPE_TEXT:
            case self::INPUT_TYPE_MOBILE:
            case self::INPUT_TYPE_TEXTAREA:
            case self::INPUT_TYPE_REGIONAL_LINKAGE:
                // 增加复制按钮
                $attributeSupport->setCustomAttribute(['copy', true]);
                break;
            case self::INPUT_VIDEO:
            case self::INPUT_TYPE_VIDEO_RADIO:
            case self::INPUT_TYPE_VIDEO_CHECK_BOX:
//                if ($rejectCount == 0 && in_array($userId, $dealUids)) {
//                    // 增加驳回按钮
//                    $attributeSupport->setCustomAttribute(['reject', true]);
//                }

                $attributeSupport->setCustomAttribute(['rejectOptions', self::REJECT_OPTIONS_VIDEO]);
                break;
            case self::INPUT_TYPE_IMG:
            case self::INPUT_TYPE_IMG_GROUP:
            case self::INPUT_TYPE_ALBUM_RADIO:
            case self::INPUT_TYPE_ALBUM_CHECKBOX:
            case self::INPUT_TYPE_CAMERA_RADIO:
            case self::INPUT_TYPE_CAMERA_CHECKBOX:
//                if ($rejectCount == 0 && in_array($userId, $dealUids)) {
//                    // 增加驳回按钮
//                    $attributeSupport->setCustomAttribute(['reject', true]);
//                }

                $attributeSupport->setCustomAttribute(['rejectOptions', self::REJECT_OPTIONS_IMAGE]);
                break;
        }

        if ($reject) {
            // 驳回内容回显
            $attributeSupport->setCustomAttribute([
                'rejectValue',
                ['desc' => $reject['reasonText'] ?? '', 'reason' => $reject['reasonOptionText'] ?? '']
            ]);
            $attributeSupport->setCustomAttribute(['reject', false]);
        }

        return $attributeSupport;
    }

    public function build(): array
    {
        self::doReject($this);

        return parent::build();
    }
}
